package cn.iocoder.boot.controller.admin;

import cn.iocoder.boot.entity.ComponentStock;
import cn.iocoder.boot.service.ComponentStockService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;

@RestController
@RequestMapping("/componentStock")
public class ComponentStockController {

    @Autowired
    private ComponentStockService componentStockService;

    @Operation(summary = "新增库存")
    @PostMapping("/save")
    @PermitAll
    public CommonResult<Boolean> saveComponentStock(@RequestBody ComponentStock componentStock) {
        return CommonResult.success(componentStockService.insertComponentStock(componentStock));
    }


    @Operation(summary = "导入库存")
    @PostMapping("/import")
    @PermitAll
    public CommonResult<Boolean> importComponentStock(@RequestParam("file") MultipartFile file) throws IOException {
        return CommonResult.success(componentStockService.importComponentStock(file));
    }




    @Operation(summary = "修改库存")
    @PostMapping("/update")
    @PermitAll
    public CommonResult<Boolean> updateComponentStock(@RequestBody ComponentStock componentStock) {
        return CommonResult.success(componentStockService.updateComponentStock(componentStock));
    }

    @PostMapping("/delete/{id}")
    public CommonResult<Boolean> deleteComponentStock(@PathVariable Long id) {
        return CommonResult.success(componentStockService.deleteComponentStock(id));
    }

    @GetMapping("/get/{id}")
    public CommonResult<ComponentStock> getComponentStock(@PathVariable Long id) {
        return CommonResult.success(componentStockService.getComponentStock(id));
    }

    @GetMapping("/list")
    public CommonResult<PageResult<ComponentStock>> listComponentStocks(PageParam pageParam, @RequestParam(required = false) String keyword) {
        Wrapper<ComponentStock> queryWrapper = null;
        if (keyword != null) {
            queryWrapper = new QueryWrapper<ComponentStock>().lambda().like(ComponentStock::getModel, keyword);
        }
        return CommonResult.success(componentStockService.listComponentStocks(pageParam, queryWrapper));
    }







    @PermitAll
    @GetMapping("/get-template")
    @Operation(summary = "获得导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 使用相对路径引用资源文件
        String fileName = "线下库存价格导入模板.xlsx";
        // 获取文件所在的路径
        ClassPathResource resource = new ClassPathResource("线下库存价格导入模板.xlsx");

        if (resource.exists()) {
            // 设置响应的内容类型为 Excel 文件
            response.setContentType("application/vnd.ms-excel");
            // 设置响应头，告诉浏览器这是一个附件并指定文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + "导入模板");

            // 获取文件输入流并进行输出
            try (InputStream in = resource.getInputStream();
                 OutputStream out = response.getOutputStream()) {

                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.flush();
            }
        } else {
            // 文件不存在时，可以做适当的错误处理
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "模板文件未找到");
        }
    }


}
